package com.neusoft.elm.dao.impl;

import com.neusoft.elm.dao.UserDao;
import com.neusoft.elm.po.User;
import com.neusoft.elm.util.DBUtil;
import com.sun.org.apache.bcel.internal.generic.DUP;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoimpl implements UserDao {
    private Connection conn = null;
    private PreparedStatement prep = null;
    private ResultSet rs = null;

    @Override//同理测试类传入user，是先导入数据库（set）再返回后端，查看是否添加成功
    //若传入成功， int n = prep.executeUpdate();值肯定为1
    public int saveUser(User user) throws Exception {
        try {
            conn = DBUtil.getConnection();
            String sql = "insert into user values(?,?,?,?,?,1)";
            prep = conn.prepareStatement(sql);
            //设置参数userId  password  userName   userSex  userImg  delTage
            prep.setString(1, user.getUserId());
            prep.setString(2, user.getPassword());
            prep.setString(3, user.getUserName());
            prep.setInt(4, user.getUserSex());
            prep.setString(5, user.getUserImg());
            int n = prep.executeUpdate();
            return n;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(prep);
        }
    }

    @Override//测试类中传入userId和password，结果集rs中找传入对应的数据是否存在
    //存在即user!=null,存在即user=null;分别输出不同的东西
    //45-47，传入的数据成了一个sql，->预编译->结果集
    //返回类型是User类型  后端和数据库的连接
    public User getUserByIdByPass(String userId, String password) throws Exception {
        try {
            conn=DBUtil.getConnection();
            String sql="select * from user where userId=? and password=?";
            prep=conn.prepareStatement(sql);
            //conn String pre三部曲，搞完之后若是select，有？的话再写
            prep.setString(1,userId);
            prep.setString(2,password);
            rs=prep.executeQuery();//到数据库中找去了
            //查询都是数据库已经有的，所以是结果集对象，rs
            //insert这种是数据库中没有的，是预编译的对象
            User user=null;
            if(rs.next()){
                user=new User();
                user.setUserId(rs.getString("userId"));
                // public void setUserName(String userName) {this.userName = userName};
                user.setPassword(rs.getString("password"));
                user.setUserName(rs.getString("userName"));
                user.setUserSex(rs.getInt("userSex"));
                user.setUserImg(rs.getString("userImg"));
                user.setDelTag(rs.getInt("delTaf"));

            }
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs, prep);
        }
    }

    @Override
    public User getUserById(String userId) throws Exception {
        try {
            conn = DBUtil.getConnection();
            String sql="select * from user where userId=?";
            prep=conn.prepareStatement(sql);
            prep.setString(1,userId);
            rs=prep.executeQuery();
            User user=null;
            if(rs.next()){
                user=new User();
                user.setUserId(rs.getString("userId"));
                // public void setUserName(String userName) {this.userName = userName};
                user.setPassword(rs.getString("password"));
                user.setUserName(rs.getString("userName"));
                user.setUserSex(rs.getInt("userSex"));
                user.setUserImg(rs.getString("userImg"));
                user.setDelTag(rs.getInt("delTaf"));

            }
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs,prep);
        }
    }
}
